var app = getApp()
var {formatDate} = require('../../../utils/util')

Page({
  data: {
    datetypes: ['公历', '阴历'],
    showQuestion: 0
  },
  onLoad: function () {
    this.loadMemory()
    var endtime = formatDate(new Date, 'yyyy-MM-dd')
    this.setData({
      endtime: endtime
    })
  },

  loadMemory: function () {
    var url = '/silo/h5/bigday/list/2511.json'
    var params = {
      auth: app.globalData.authToken
    }
    app.request(url, params).then(res => {
      if (res.protocError === 0) {
        var items = res.items
        var normalnum = res.maxNormal
        var vip_memory = [], normal_memory = []
        for (var i = 0; i < items.length; i++) {
          items[i].dateval = items[i].year + '-' + items[i].month + '-' + items[i].day
          if (!items[i].title) {
            items[i].title = ''
          }
        }
        this.setData({
          memorylist: items,
          maxNormal: normalnum
        })
      }
    })
  },
  bindDateChange: function (e) {
    var dateval = e.detail.value
    var mindex = e.currentTarget.dataset.index
    var vals = dateval.split('-')
    var memory = this.data.memorylist
    memory[mindex].year = parseInt(vals[0])
    memory[mindex].month = parseInt(vals[1])
    memory[mindex].day = parseInt(vals[2])
    this.setData({
      memorylist: memory
    })
  },
  dateTypeChange: function (e) {
    var memory = this.data.memorylist
    var mindex = e.currentTarget.dataset.index
    memory[mindex].lunar = e.detail.value == 0 ? false : true
    this.setData({
      memorylist: memory
    })
  },
  nameInput: function (e) {
    var mindex = e.currentTarget.dataset.index
    var memory = this.data.memorylist
    memory[mindex].title = e.detail.value
    this.setData({
      memorylist: memory
    })
  },
  saveMemoryAction: function () {
    app.showLoading()
    var memory = this.data.memorylist
    for (var i = 0; i < memory.length; i++) {
      if (!memory.title) {
        memory.title = ''
      }
    }
    var url = '/silo/h5/bigday/update/2513.json'
    var params = {
      auth: app.globalData.authToken,
      items: this.data.memorylist
    }
    app.request(url, params).then(res => {
      app.hideLoading()
      if (res.protocError === 0) {
        app.toast.success('保存成功')
      }
    })
  },
  showQuestionAction: function () {
    this.setData({
      showQuestion: 1
    })
  },
  closeQuestionAction: function () {
    this.setData({
      showQuestion: 0
    })
  }
})
